home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / pythonconsole / config.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  4.0 KB  |  121 lines

  1. # -*- coding: utf-8 -*-
  2.  
  3. # config.py -- Config dialog
  4. #
  5. # Copyright (C) 2008 - B. Clausius
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2, or (at your option)
  10. # any later version.
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  
  20. # Parts from "Interactive Python-GTK Console" (stolen from epiphany's console.py)
  21. #     Copyright (C), 1998 James Henstridge <james@daa.com.au>
  22. #     Copyright (C), 2005 Adam Hooper <adamh@densi.com>
  23. # Bits from gedit Python Console Plugin
  24. #     Copyrignt (C), 2005 Rapha√´l Slinckx
  25.  
  26. import os
  27.  
  28. import gtk
  29. import gconf
  30.  
  31. __all__ = ('PythonConsoleConfig', 'PythonConsoleConfigDialog')
  32.  
  33. GCONF_KEY_BASE = '/apps/gedit-2/plugins/pythonconsole'
  34. GCONF_KEY_COMMAND_COLOR = GCONF_KEY_BASE + '/command-color'
  35. GCONF_KEY_ERROR_COLOR = GCONF_KEY_BASE + '/error-color'
  36.  
  37. DEFAULT_COMMAND_COLOR = '#314e6c' # Blue Shadow
  38. DEFAULT_ERROR_COLOR = '#990000' # Accent Red Dark
  39.  
  40. class PythonConsoleConfig (object):
  41.  
  42.     def __init__(self):
  43.         pass
  44.     
  45.     @staticmethod
  46.     def add_handler(handler):
  47.         gconf.client_get_default().notify_add(GCONF_KEY_BASE, handler)
  48.  
  49.     color_command = property(
  50.         lambda self:  self.gconf_get_str(GCONF_KEY_COMMAND_COLOR, DEFAULT_COMMAND_COLOR),
  51.         lambda self, value:  self.gconf_set_str(GCONF_KEY_COMMAND_COLOR, value))
  52.  
  53.     color_error = property(
  54.         lambda self:  self.gconf_get_str(GCONF_KEY_ERROR_COLOR, DEFAULT_ERROR_COLOR),
  55.         lambda self, value:  self.gconf_set_str(GCONF_KEY_ERROR_COLOR, value))
  56.     
  57.     @staticmethod
  58.     def gconf_get_str(key, default=''):
  59.         val = gconf.client_get_default().get(key)
  60.         if val is not None and val.type == gconf.VALUE_STRING:
  61.             return val.get_string()
  62.         else:
  63.             return default
  64.  
  65.     @staticmethod
  66.     def gconf_set_str(key, value):
  67.         v = gconf.Value(gconf.VALUE_STRING)
  68.         v.set_string(value)
  69.         gconf.client_get_default().set(key, v)
  70.  
  71. class PythonConsoleConfigDialog (object):
  72.  
  73.     def __init__(self, datadir):
  74.         object.__init__(self)
  75.         self._dialog = None
  76.         self._ui_path = os.path.join(datadir, 'ui', 'config.ui')
  77.         self.config = PythonConsoleConfig()
  78.  
  79.     def dialog(self):
  80.         if self._dialog is None:
  81.             self._ui = gtk.Builder()
  82.             self._ui.add_from_file(self._ui_path)
  83.  
  84.             self.set_colorbutton_color(self._ui.get_object('colorbutton-command'),
  85.                                         self.config.color_command)
  86.             self.set_colorbutton_color(self._ui.get_object('colorbutton-error'),
  87.                                         self.config.color_error)
  88.             
  89.             self._ui.connect_signals(self)
  90.             
  91.             self._dialog = self._ui.get_object('dialog-config')
  92.             self._dialog.show_all()
  93.         else:
  94.             self._dialog.present()
  95.         
  96.         return self._dialog
  97.     
  98.     @staticmethod
  99.     def set_colorbutton_color(colorbutton, value):
  100.         try:
  101.             color = gtk.gdk.color_parse(value)
  102.         except ValueError:
  103.             pass    # Default color in config.ui used
  104.         else:
  105.             colorbutton.set_color(color)
  106.  
  107.     def on_dialog_config_response(self, dialog, response_id):
  108.         self._dialog.destroy()
  109.  
  110.     def on_dialog_config_destroy(self, dialog):
  111.         self._dialog = None
  112.         self._ui = None
  113.         
  114.     def on_colorbutton_command_color_set(self, colorbutton):
  115.         self.config.color_command = colorbutton.get_color().to_string()
  116.  
  117.     def on_colorbutton_error_color_set(self, colorbutton):
  118.         self.config.color_error = colorbutton.get_color().to_string()
  119.  
  120.